<?php

namespace JLPay\Models\Ledger\Merchconf;

use JLPay\Models\BaseResponse;

/**
 * 商户分账查询响应
 */
class MerchIssueQueryResponse extends BaseResponse
{
    protected ?string $orgCode = null;
    protected ?string $mchId = null;
    protected ?string $ledgerModule = null;
    protected ?string $maxSplitRate = null;
    protected ?string $sourceIds = null;
    protected ?string $feeRate = null;
    protected ?string $pbthf = null;
    protected ?string $lowAmt = null;
    protected ?string $upAmt = null;
    protected ?string $notifyUrl = null;
    protected ?array $binds = null;

    /**
     * 构造函数
     *
     * @param array|null $responseArray
     */
    public function __construct(?array $responseArray = null)
    {
        parent::__construct($responseArray);

        if ($responseArray) {
            $this->orgCode = $responseArray['org_code'] ?? null;
            $this->mchId = $responseArray['mch_id'] ?? null;
            $this->ledgerModule = $responseArray['ledger_module'] ?? null;
            $this->maxSplitRate = $responseArray['max_split_rate'] ?? null;
            $this->sourceIds = $responseArray['source_ids'] ?? null;
            $this->feeRate = $responseArray['fee_rate'] ?? null;
            $this->pbthf = $responseArray['pbthf'] ?? null;
            $this->lowAmt = $responseArray['low_amt'] ?? null;
            $this->upAmt = $responseArray['up_amt'] ?? null;
            $this->notifyUrl = $responseArray['notify_url'] ?? null;
            
            // 处理binds数组
            $bindsData = $responseArray['binds'] ?? [];
            $this->binds = [];
            foreach ($bindsData as $bindArray) {
                $bind = new Bind($bindArray);
                $this->binds[] = $bind;
            }
        }
    }

    /**
     * 获取机构代码
     *
     * @return string|null
     */
    public function getOrgCode(): ?string
    {
        return $this->orgCode;
    }

    /**
     * 获取商户号
     *
     * @return string|null
     */
    public function getMchId(): ?string
    {
        return $this->mchId;
    }

    /**
     * 获取账本模块
     *
     * @return string|null
     */
    public function getLedgerModule(): ?string
    {
        return $this->ledgerModule;
    }

    /**
     * 获取最大分账比例
     *
     * @return string|null
     */
    public function getMaxSplitRate(): ?string
    {
        return $this->maxSplitRate;
    }

    /**
     * 获取来源ID列表
     *
     * @return string|null
     */
    public function getSourceIds(): ?string
    {
        return $this->sourceIds;
    }

    /**
     * 获取费率
     *
     * @return string|null
     */
    public function getFeeRate(): ?string
    {
        return $this->feeRate;
    }

    /**
     * 获取保底后收费
     *
     * @return string|null
     */
    public function getPbthf(): ?string
    {
        return $this->pbthf;
    }

    /**
     * 获取最低金额
     *
     * @return string|null
     */
    public function getLowAmt(): ?string
    {
        return $this->lowAmt;
    }

    /**
     * 获取最高金额
     *
     * @return string|null
     */
    public function getUpAmt(): ?string
    {
        return $this->upAmt;
    }

    /**
     * 获取通知URL
     *
     * @return string|null
     */
    public function getNotifyUrl(): ?string
    {
        return $this->notifyUrl;
    }

    /**
     * 获取绑定列表
     *
     * @return array|null
     */
    public function getBinds(): ?array
    {
        return $this->binds;
    }

    /**
     * 转换为数组
     *
     * @return array
     */
    public function toArray(): array
    {
        $data = parent::toArray();
        
        // 正确处理binds字段
        if ($this->binds) {
            $data['binds'] = [];
            foreach ($this->binds as $bind) {
                $data['binds'][] = $bind->toArray();
            }
        }
        
        return $data;
    }
} 